import { createRouter, createWebHistory } from 'vue-router'
import login from "@/views/Login.vue"
import ManageBase from "@/views/manage/Base.vue"
import manageRoute from '@/router/manageRoute'
import { Notification } from '@arco-design/web-vue';
import { get } from "@/service/baseService.js"
const IsLogin = (to, from, next) => {
    // 身份验证
    // console.log("身份验证的咯！");
    var token = localStorage.getItem("token")
    if(token){
        get("is/login").then((res)=>{
            if(res.data.code == 200){
                next()
            }else if(res.data.code == 101){
                Notification.error({
                    title: '错误提示',
                    content: res.data.message,
                })
                localStorage.removeItem("token")
                Router.push("/login")
            }else{
                Notification.error({
                    title: '错误提示',
                    content: "未知错误！",
                })
            }
        }).catch((res)=>{
            console.log(res);
            Notification.error({
                title: '错误提示',
                content: "网络或服务器异常！",
            })
            Router.push("/login")
        })
    }else{
        Notification.error({
            title: '系统提示',
            content: "您还未登录，请先登录~",
        })
        Router.push("/login")
    }
}

const route = [
    {
        path: "/",
        redirect: "/manage"
    },
    {
        path: "/login",
        component: login
    },
    {
        path: "/manage",
        beforeEnter: IsLogin,
        children: manageRoute,
        component: ManageBase,
        redirect: "/manage/index"
    }
]

const Router = createRouter({
    history: createWebHistory(),
    routes: route
})

export default Router